﻿Imports DAO
Imports BUS
Imports DTO

Public Class FormPhieuThuTien

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

#Region "Biến toàn cục"
    Private flag As Integer = 0
    Private flaghople As Integer = 0
    Private tienno As Double = 0
#End Region

#Region "Load form"
    Private Sub FormPhieuThuTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt = PhieuThuTien_BUS.LayCMND()
        ComboBoxCMND.DataSource = dt
        ComboBoxCMND.DisplayMember = "CMND"
        ComboBoxCMND.ValueMember = "MaKH"
        ComboBoxCMND.SelectedIndex = -1
        flag = 1
    End Sub
#End Region

#Region "Clear các textbox"
    Private Sub Clear()
        TextBoxTenKH.Clear()
        TextBoxDiaChi.Clear()
        TextBoxDienThoai.Clear()
        TextBoxEmail.Clear()
        TextBoxSoTienThu.Clear()
        TextBoxTienNo.Clear()
        CheckBoxQuyDinh.CheckState = CheckState.Checked
    End Sub
#End Region

#Region "Xử lý chọn CMND"
    Private Sub ChonCMND()
        Try
            Dim cmnd As String = ComboBoxCMND.Text
            Dim tb As DataTable = PhieuThuTien_BUS.LayKhachHang(cmnd)
            TextBoxTenKH.Text = tb.Rows(0).Item(1).ToString()
            TextBoxDiaChi.Text = tb.Rows(0).Item(2).ToString()
            TextBoxDienThoai.Text = tb.Rows(0).Item(3).ToString()
            TextBoxEmail.Text = tb.Rows(0).Item(4).ToString()
            TextBoxTienNo.Text = tb.Rows(0).Item(6).ToString()
            tienno = tb.Rows(0).Item(6).ToString()
            flaghople = 1
        Catch ex As Exception
            MessageBox.Show("Khách hàng không tồn tại", "Thông báo")
            flaghople = 0
            ComboBoxCMND.Focus()
            Clear()
        End Try
    End Sub
#End Region


#Region "Kiểm tra dữ liệu textBox số lượng"
    Public Function KTSL(ByVal sl As String) As Boolean
        If TextBoxSoTienThu.Text = "" Then
            Return False
        End If

        Dim i As Integer
        For i = 0 To 1
            If (IsNumeric(sl(i)) = False) Then
                Return False
            End If
        Next
        Return True
    End Function
#End Region

#Region "Xử lý nút lập phiếu thu"
    Private Sub ButtonPhieuThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPhieuThu.Click
        If KTSL(TextBoxSoTienThu.Text) = True Then
            If flaghople = 1 Then
                Dim tienthu As Double = Double.Parse(TextBoxSoTienThu.Text)
                If tienthu * flagquydinh <= Math.Abs(tienno) Or tienno <= 0 Then
                    Dim PhieuThu As New PhieuThu_DTO(DTPNgayTT.Value, ComboBoxCMND.SelectedValue, TextBoxSoTienThu.Text)
                    Dim pt As New PhieuThuTien_BUS()
                    Dim flag As Integer
                    flag = pt.NhapPhieuThu(PhieuThu)
                    If (flag > 0) Then
                        MessageBox.Show("Lập phiếu thu tiền THÀNH CÔNG", "Thông báo")
                        ComboBoxCMND.Text = ""
                        Clear()
                    Else
                        MessageBox.Show("Lập phiếu thu tiền THẤT BẠI", "Thông báo")
                    End If
                Else
                    MessageBox.Show("Tiền thu vượt quá số tiền nợ của khách hàng", "Thông báo")
                    TextBoxSoTienThu.Focus()
                End If
            Else
                MessageBox.Show("Chưa chọn khách hàng", "Thông báo")
            End If
        Else
            MessageBox.Show("Số tiền thu không hợp lệ", "Thông báo")
            TextBoxSoTienThu.Focus()
        End If
    End Sub
#End Region

    Private Sub ComboBoxCMND_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxCMND.Leave
        ChonCMND()
    End Sub

    Private Sub ComboBoxCMND_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxCMND.SelectedIndexChanged
        If flag = 1 Then
            ChonCMND()
        End If
    End Sub

    Private flagquydinh As Integer = 1

    Private Sub CheckBoxQuyDinh_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxQuyDinh.CheckedChanged
        If CheckBoxQuyDinh.Checked Then
            flagquydinh = 1
        Else
            flagquydinh = -1
        End If
    End Sub

End Class